package edu.study.user.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;

import edu.study.api.common.dto.BaseRespDto;
import edu.study.api.common.dto.PageRespDto;
import edu.study.api.common.dto.RespDto;
import edu.study.api.common.enums.BaseResultCodeEnum;
import edu.study.api.common.enums.StatusTypeEnum;
import edu.study.common.util.BeanCopy;
import edu.study.common.util.cache.Cache;
import edu.study.user.api.dto.RoleDto;
import edu.study.user.api.dto.UserDetailDto;
import edu.study.user.api.req.role.AddRoleReq;
import edu.study.user.api.req.role.DeleteRoleReq;
import edu.study.user.api.req.role.ModifyRoleReq;
import edu.study.user.api.req.role.ModifyRoleStatusReq;
import edu.study.user.api.req.role.QueryRolePageListReq;
import edu.study.user.api.req.role.QueryRoleReq;
import edu.study.user.dao.mapperExt.DepartmentDOMapperExt;
import edu.study.user.dao.mapperExt.RoleDOMapperExt;
import edu.study.user.dbBean.DepartmentDO;
import edu.study.user.dbBean.RoleDO;
import edu.study.user.util.UserUtils;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class RoleService {

	@Autowired
	private RoleDOMapperExt roleDOMapperExt;
	
	@Autowired
	private DepartmentDOMapperExt departmentDOMapperExt;
	
	@Autowired
	private Cache cache;

	public BaseRespDto addRole(AddRoleReq req) {
		BaseRespDto resp = new BaseRespDto();
		
		DepartmentDO departmentDO = departmentDOMapperExt.queryByDepartmentNo(req.getDepartmentNo());
		if(departmentDO == null) {
			log.info("RoleService.addRole, 部门不存在! departmentNo={}", req.getDepartmentNo());
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		RoleDO checkRoleDO = roleDOMapperExt.queryRoleForCheck(req.getRoleName(), req.getDepartmentNo());
		if (checkRoleDO != null) {
			log.info("RoleService.addRole, 同一个部门下面的角色不允许名称一样! departmentNo={}, roleName={}", req.getDepartmentNo(), req.getRoleName());
			resp.setResultFail(BaseResultCodeEnum.RECORD_ALREADY_EXISTS);
			return resp;
		}
		
		RoleDO newRoleDO = new RoleDO();
		BeanCopy.copyFromSrcToDest(req, newRoleDO);
		String roleNo = cache.generateStr("roleNo_", 1, null);
		newRoleDO.setRoleNo(roleNo);
		UserDetailDto userDetailDto = UserUtils.getCurrentUser();
		if(userDetailDto!=null) {
			newRoleDO.setCreator(userDetailDto.getLoginName());
			newRoleDO.setModifier(userDetailDto.getLoginName());
		}
		int i = roleDOMapperExt.insertSelective(newRoleDO);
		if (i == 0) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}

	public BaseRespDto deleteRole(DeleteRoleReq req) {
		BaseRespDto resp = new BaseRespDto();

		RoleDO roleDO = roleDOMapperExt.queryByRoleNo(req.getRoleNo());
		if (roleDO == null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		//todo 删除了，关联的数据怎么处理？？？
		RoleDO newRoleDO = new RoleDO();
		newRoleDO.setId(roleDO.getId());
		newRoleDO.setIsDeleted(StatusTypeEnum.Y.getCode());
		int i = roleDOMapperExt.updateByPrimaryKeySelective(newRoleDO);
		if (i == 0) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto modifyRole(ModifyRoleReq req) {
		BaseRespDto resp = new BaseRespDto();

		RoleDO roleDO = roleDOMapperExt.queryByRoleNo(req.getRoleNo());
		if (roleDO == null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		
		//校验有没有修改角色名称
		if(!StringUtils.isEmpty(req.getRoleName()) && !roleDO.getRoleName().equals(req.getRoleName())) {
			RoleDO checkRoleDO = roleDOMapperExt.queryRoleForCheck(req.getRoleName(), roleDO.getDepartmentNo());
			if (checkRoleDO != null) {
				log.info("RoleService.addRole, 同一个部门下面的角色不允许名称一样! departmentNo={}, roleName={}", roleDO.getDepartmentNo(), req.getRoleName());
				resp.setResultFail(BaseResultCodeEnum.RECORD_ALREADY_EXISTS);
				return resp;
			}
		}
		
		RoleDO newRoleDO = new RoleDO();
		BeanCopy.copyFromSrcToDest(req, newRoleDO);
		newRoleDO.setRoleNo(null);
		newRoleDO.setId(roleDO.getId());
		UserDetailDto userDetailDto = UserUtils.getCurrentUser();
		if(userDetailDto!=null) {
			newRoleDO.setModifier(userDetailDto.getLoginName());
		}
		int i = roleDOMapperExt.updateByPrimaryKeySelective(newRoleDO);
		if (i == 0) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public BaseRespDto modifyRoleStatus(ModifyRoleStatusReq req) {
		BaseRespDto resp = new BaseRespDto();

		RoleDO roleDO = roleDOMapperExt.queryByRoleNo(req.getRoleNo());
		if (roleDO == null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		if(req.getStatus().equals(roleDO.getStatus())) {
			log.info("RoleService.modifyRoleStatus, 不用修改, roleNo={}", req.getRoleNo());
			return resp;
		}
		//todo 失效了，关联的数据怎么处理？？？
		RoleDO newRoleDO = new RoleDO();
		newRoleDO.setId(roleDO.getId());
		newRoleDO.setStatus(req.getStatus());
		UserDetailDto userDetailDto = UserUtils.getCurrentUser();
		if(userDetailDto!=null){
			newRoleDO.setModifier(userDetailDto.getLoginName());
		}
		int i = roleDOMapperExt.updateByPrimaryKeySelective(newRoleDO);
		if (i == 0) {
			resp.setResultFail(BaseResultCodeEnum.DB_ERROR);
			return resp;
		}
		return resp;
	}
	
	public RespDto<RoleDto> queryRole(QueryRoleReq req) {
		RespDto<RoleDto> resp = new RespDto<RoleDto>();

		RoleDO roleDO = roleDOMapperExt.queryByRoleNo(req.getRoleNo());
		if (roleDO == null) {
			resp.setResultFail(BaseResultCodeEnum.RECORD_NOT_FOUND);
			return resp;
		}
		RoleDto roleDto = new RoleDto();
		BeanCopy.copyFromSrcToDest(roleDO, roleDto);
		resp.setData(roleDto);
		return resp;
	}
		
	public PageRespDto<RoleDto> queryRolePageList(QueryRolePageListReq req) {
		PageRespDto<RoleDto> resp = new PageRespDto<RoleDto>();
		
		Page<RoleDto> roleDtoPageList = roleDOMapperExt.queryRolePageList(req);
		if (roleDtoPageList != null && !CollectionUtils.isEmpty(roleDtoPageList.getResult())) {
			resp.setPages(roleDtoPageList.getPages());
			resp.setTotal(roleDtoPageList.getTotal());
			resp.setData(roleDtoPageList.getResult());
		}
		return resp;
	}
}
